package com.yndsoft.jc.supplier.info;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.AccountModel;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.utils.UiUtils;

import org.greenrobot.eventbus.EventBus;

public class SupplierAccount extends SupplierBaseActivity implements View.OnClickListener {

    private TextView infoBank;
    private TextView infoAli;
    private TextView infoWx;
    private TextView infoBankTitle;
    private EditText infoBankName;
    private EditText infoBankUser;
    private EditText infoBankAccount;
    private LinearLayout infoOther;
    private int collectStatus = 1; // 1支付宝 2微信 3银行

    @Override
    public int getContentResId() {
        return R.layout.supplier_account;
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, SupplierAccount.class));
    }

    @Override
    public void initView() {

        ImageButton accountClose = findViewById(R.id.account_close);
        infoBank = findViewById(R.id.info_bank);
        infoAli = findViewById(R.id.info_ali);
        infoWx = findViewById(R.id.info_wx);
        infoBankTitle = findViewById(R.id.info_bank_title);
        infoBankName = findViewById(R.id.info_bank_name);
        infoBankUser = findViewById(R.id.info_bank_user);
        infoBankAccount = findViewById(R.id.info_bank_account);
        infoOther = findViewById(R.id.info_other);
        Button accountConfirm = findViewById(R.id.account_confirm);

        accountClose.setOnClickListener(this);
        infoBank.setOnClickListener(this);
        infoAli.setOnClickListener(this);
        infoWx.setOnClickListener(this);
        accountConfirm.setOnClickListener(this);
        UiUtils.initSoftInputShow(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.account_close:
                finish();
                break;
            case R.id.info_bank:
                collectStatus = 3;
                isCheck(infoBank, true);
                isCheck(infoAli, false);
                isCheck(infoWx, false);
                infoBankTitle.setText("开户行");
                infoBankName.setHint("请输入开户行名称");
                infoOther.setVisibility(View.VISIBLE);
                break;
            case R.id.info_ali:
                collectStatus = 1;
                isCheck(infoBank, false);
                isCheck(infoAli, true);
                isCheck(infoWx, false);
                infoBankTitle.setText("账号");
                infoBankName.setHint("请输入支付宝账号");
                infoOther.setVisibility(View.GONE);
                break;
            case R.id.info_wx:
                collectStatus = 2;
                isCheck(infoBank, false);
                isCheck(infoAli, false);
                isCheck(infoWx, true);
                infoBankTitle.setText("账号");
                infoBankName.setHint("请输入微信账号");
                infoOther.setVisibility(View.GONE);
                break;
            case R.id.account_confirm:
                String bankNameValue = infoBankName.getText().toString().trim();
                String bankUserValue = infoBankUser.getText().toString().trim();
                String bankAccountValue = infoBankAccount.getText().toString().trim();
                switch (collectStatus) {
                    case 3: // 银行
                        if (TextUtils.isEmpty(bankNameValue)) {
                            showHint("请输入开户行名称");
                        } else {
                            if (TextUtils.isEmpty(bankUserValue)) {
                                showHint("请输入银行开户名");
                            } else {
                                if (TextUtils.isEmpty(bankAccountValue)) {
                                    showHint("请输入公司银行账号");
                                } else {
                                    putCause(bankNameValue, bankUserValue, bankAccountValue);
                                }
                            }
                        }
                        break;
                    case 1: // 支付宝
                        if (TextUtils.isEmpty(bankNameValue)) {
                            showHint("请输入支付宝账号");
                        } else {
                            putCause(bankNameValue, null, null);
                        }
                        break;
                    case 2: // 微信
                        if (TextUtils.isEmpty(bankNameValue)) {
                            showHint("请输入微信账号");
                        } else {
                            putCause(bankNameValue, null, null);
                        }
                        break;
                }
                break;
        }
    }

    private void isCheck(TextView textView, boolean isCheck) {
        Drawable drawable;
        if (isCheck) {
            drawable = getResources().getDrawable(R.mipmap.checked);

        } else {
            drawable = getResources().getDrawable(R.mipmap.unchecked);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    private void putCause(String bankNameValue, String bankUserValue, String bankAccountValue) {
        finish();
        AccountModel accountModel = new AccountModel();
        accountModel.setFunction(1);
        accountModel.setStatus(collectStatus);
        switch (collectStatus) {
            case 3:
                accountModel.setName(bankNameValue);
                accountModel.setUser(bankUserValue);
                accountModel.setAccount(bankAccountValue);
                break;
            case 1:
                accountModel.setName(bankNameValue);
                break;
            case 2:
                accountModel.setName(bankNameValue);
                break;
        }
        EventBus.getDefault().post(accountModel);
    }
}
